<?php
Yii::import('lib.widgets.tree.CNodePath');
/**
 * 节点路径链接控件
 * <pre>
 * $this->widget('lib.widgets.tree.CNodePathLinker', array(
 *  // 当前节点
 *  'model' => EnumPca::model()->findByPk(5),
 *  // 取值字段，用于显示
 *  'valueAttribute' => 'name',
 *  // 创建URL时所需的参数
 *  'createUrl' => array('/a/b/c', array('id'=>'{id}')),
 * ));
 * </pre>
 * @author yangdongqi <yangdongqi@hayzone.com>
 * @package lib.widgets.tree
 */
class CNodePathLinker extends CNodePath {
	
	/**
	 * 创建url时所需要的参数
	 * <pre>
	 * e.g. array('/a/b/c', array(
	 *  'id' => '{id}',
	 * ));
	 * </pre>
	 * 其中{}所包裹的字符将被替换为$model->{id}所对应的值
	 * @var array
	 */
	public $createUrl = array();
	
	/**
	 * 创建链接所需要的htmlOptions
	 * @var array
	 */
	public $linkerHtmlOptions = array();
	
	protected function createNodeHtml($node) {
		list($route, $paramsTpl) = $this->createUrl;
		$params = array();
		foreach($paramsTpl as $k=>$v) {
			preg_match_all('/{(\w+)}/', $v, $matches);
			foreach($matches[1] as $match) {
				$v = str_replace('{' . $match . '}', $node->$match, $v);
			}
			$params[$k] = $v;
		}
		
		$url = $this->getController()->createUrl($route, $params);
		return CHtml::link(
			$node->{$this->valueAttribute}, 
			$url, 
			$this->linkerHtmlOptions
		);
	}
	
}

?>